package com.android.place360.current_location;

import android.content.Context;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;
import com.android.place360.R;

public class MyLocationManager{
	private static LocationManager mlocManager;
	private static MyLocationListener mlocListener;
	public static String provider;
	public static boolean StartLocatonListener(Context context, TextView textView){
		try {

			mlocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

			boolean gps_enable = false;
			boolean network_enable = false;
			
			try{
				network_enable = mlocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			try {
				gps_enable = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			if(!network_enable && !gps_enable) return false;
			
			provider = LocationManager.GPS_PROVIDER;
			//if(network_enable) provider = LocationManager.NETWORK_PROVIDER;
			
			mlocListener = MyLocationListener.getInstance(context, textView);	
			mlocManager.requestLocationUpdates(provider, 10000, 0, mlocListener);
			Location loc = mlocManager.getLastKnownLocation(provider);
			
			//Location Error Alert
			if(loc==null)
				Toast.makeText(context, context.getString(R.string.current_location_error), Toast.LENGTH_LONG).show();
			
			mlocListener.setLocation(loc);
			return true;
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public static void stopLocationListener(){
		try{
			mlocManager.removeUpdates(mlocListener);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String getCurrentAddressString(){
		return mlocListener.getTxtLocation().getText().toString();
	}
	
	public static void setLocation(Location loc){
		if(mlocListener!=null)
			mlocListener.setLocation(loc);
	}
	public static void setLocation(Address add){
		if(mlocListener!=null)
			mlocListener.setLocation(add);
	}
	public static void updateLocationLanguage(){
		if(mlocListener!=null)
			mlocListener.updateLocationLanguage();
	}
}
